package com.alen.sun.dao;

import com.alen.sun.bean.Elderly;
import com.alen.sun.bean.Employee;
import com.alen.sun.bean.Folk;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ElderlyMapper {

    @Select("select * from elderly")
    List<Elderly> list();

    @Select("select * from elderly where phoneNum=#{phoneNum} and password=#{password}")
    Elderly selectPhoneNum(Elderly elderly);

    @Insert("INSERT INTO elderly(elderlyId, name, gender, phoneNum, password, age, note, roomNum, employeeId) " +
            "VALUES(#{elderlyId}, #{name}, #{gender}, #{phoneNum}, #{password}, #{age}, #{note}, #{roomNum}, #{employeeId})")
    void add(Elderly elderly);

    //查询老人id是否存在
    @Select("SELECT count(*) from elderly WHERE elderlyId = #{elderlyId}")
    Boolean selectByElderlyId(String elderlyId);

    @Select("SELECT * FROM elderly WHERE employeeId = #{employeeId}")
    List<Elderly> getElderlysForEmployee(String employeeId);

    //分配房间给老人
    @Update("UPDATE elderly SET roomNum = #{roomNum} WHERE elderlyId = #{elderlyId}")
    Boolean setRoomForElderly(Elderly elderly);

    //查看还没分配护工的老人id
    /**
     * 查询总记录数
     * @return
     */
    @Select("select count(*) from elderly WHERE employeeId = '000000' ")
    Long count();

    /**
     * 分页查询,获取列表数据
     * @param start
     * @param pageSize
     * @return
     */
    @Select("select * from elderly WHERE employeeId = '000000'  limit #{start},#{pageSize}")
    List<Elderly> page(Integer start, Integer pageSize);

    //分配护工给老人
    @Update("UPDATE elderly SET employeeId = #{employeeId} WHERE elderlyId = #{elderlyId}")
    Boolean setEmployeeIdForElderly(Elderly elderly);


    @Select("select * from elderly where elderlyId = #{id}")
    Elderly getById(String id);

    @Delete("delete from elderly where elderlyId =#{id}")
    Boolean delete(String id);

    @Update("update elderly set name = #{name},gender = #{gender},phoneNum=#{phoneNum},password=#{password}" +
            ",age=#{age},note=#{note},employeeId=#{employeeId},roomNum=#{roomNum} where elderlyId = #{elderlyId}")
    Boolean update(Elderly elderly);

    @Select("select * from elderly where name like #{word}")
    List<Elderly> selectByWord(@Param("word") String word);

    //批量删除
    @Delete({
            "<script>",
            "delete from elderly where elderlyId in",
            "<foreach item='id' collection='ids' open='(' separator=',' close=')'>#{id}</foreach>",
            "</script>"
    })
    Boolean deleteByIds(List<String> ids);

    @Select("select count(*) from elderly where employeeId=#{id}")
    int getCountByEmpId(String id);
}
